package com.gxuwz.medical.domain.recordfree;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.exception.DbException;

/**
 * 报销记录
 * 
 * @author L&P
 * 
 */
public class Recordfree {
	// 个人编号
	private String perscode;
	// 报销总金额
	private Integer freetotal;

	/**
	 * 构造函数
	 */
	public Recordfree() {

	}

	public Recordfree(String perscode, Integer freetotal) {
		this.freetotal = freetotal;
		this.perscode = perscode;
	}

	/**
	 * 
	 * @return
	 */
	public Recordfree(String perscode) throws Exception {
		this.perscode = perscode;
		try {
			loadDB();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DbException("无法找到" + perscode);
		}
	}

	private void loadDB() throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_recordfree where  perscode=?");
			int index = 1;
			pstmt.setString(index, this.perscode);
			rs = pstmt.executeQuery();
			if (rs.next()) { // 循环输出
				this.perscode = rs.getString("perscode");
				this.freetotal = rs.getInt("freetotal");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}

	public String getPerscode() {
		return perscode;
	}

	public void setPerscode(String perscode) {
		this.perscode = perscode;
	}

	public Integer getFreetotal() {
		return freetotal;
	}

	public void setFreetotal(Integer freetotal) {
		this.freetotal = freetotal;
	}

	private void saveToDB(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"INSERT INTO t_recordfree (perscode, freetotal)");
			sqlBuff.append("VALUES (?, ?)");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			int index = 1;
			pstmt.setString(index++, this.perscode);
			pstmt.setInt(index++, this.freetotal);
			int count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}
}
